from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, BooleanField, SelectField, PasswordField
from wtforms.validators import Optional, DataRequired, EqualTo, ValidationError

class NameForm(FlaskForm):
    id = StringField('请输入你的学号', validators=[Optional()])
    studentName = StringField('请输入你的姓名')
    studentBirth = StringField('请输入你的出生日期', validators=[Optional()])
    isMale = BooleanField('请选择你的性别', default=False)
    major = SelectField('请选择你的专业', coerce=int, validators=[Optional()])
    submit = SubmitField('提交')

class EditForm(NameForm):
    submit = SubmitField("修改")

class DeleteForm(FlaskForm):
    submit = SubmitField("删除")

class LoginForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired()])
    password = PasswordField('密码', validators=[DataRequired()])
    remember_me = BooleanField('记住我')
    submit = SubmitField('登录')

class RegisterForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired()])
    password = PasswordField('密码', validators=[DataRequired()])
    password2 = PasswordField('确认密码', validators=[DataRequired(), EqualTo('password', message='两次密码不一致')])
    submit = SubmitField('注册')

    def validate_username(self, username):
        from .models import User  # 避免循环导入
        user = User.query.filter_by(username=username.data).first()
        if user is not None:
            raise ValidationError('用户名已存在')